--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
meshchatx/src/backend/async_utils.py fda9187e95d8b9cc8373c1de23cefce6928c241d (fda9187e) Text, 2.27 KB
Tff7b72import T7ee787asyncio
Tff7b72import T7ee787sys
Tff7b72from T7ee787collectionsT7ee787.T7ee787abc Tff7b72import Te6edf3Coroutine
Tff7b72class T56d364AsyncUtilsTb4b4b4:
T8b949e# remember main loop
Te6edf3main_loopTb4b4b4: Te6edf3asyncioTff7b72.Td2a8ffAbstractEventLoop Tff7b72| Tff7b72None Tff7b72= Tff7b72None
Tf0883e@staticmethod
Tff7b72def Td2a8ffapply_asyncio_313_patchTb4b4b4(Tb4b4b4)Tb4b4b4:
T8b949e"""Apply a patch for asyncio on Python 3.13 to avoid a bug in sendfile with SSL.
See: https://github.com/python/cpython/issues/124448
And: https://github.com/aio-libs/aiohttp/issues/8863
"""
Tff7b72if Te6edf3sysTff7b72.Td2a8ffversion_info Tff7b72>Tff7b72= Tb4b4b4(T79c0ff3Tb4b4b4, T79c0ff13Tb4b4b4)Tb4b4b4:
Tff7b72import T7ee787asyncioT7ee787.T7ee787base_events
T8b949e# We need to patch the loop's sendfile to raise NotImplementedError for SSL transports.
T8b949e# This will force aiohttp to use its own fallback which works correctly.
Te6edf3original_sendfile Tff7b72= Te6edf3asyncioTff7b72.Td2a8ffbase_eventsTff7b72.Td2a8ffBaseEventLoopTff7b72.Td2a8ffsendfile
Tff7b72async Tff7b72def Td2a8ffpatched_sendfileTb4b4b4(
Tff7b72selfTb4b4b4,
Te6edf3transportTb4b4b4,
Te6edf3fileTb4b4b4,
Te6edf3offsetTff7b72=T79c0ff0Tb4b4b4,
Te6edf3countTff7b72=Tff7b72NoneTb4b4b4,
Tff7b72*Tb4b4b4,
Te6edf3fallbackTff7b72=Tff7b72TrueTb4b4b4,
Tb4b4b4)Tb4b4b4:
Tff7b72if Te6edf3transportTff7b72.Td2a8ffget_extra_infoTb4b4b4(Ta5d6ff"Ta5d6ffsslcontextTa5d6ff"Tb4b4b4)Tb4b4b4:
Tff7b72raise Tf85149NotImplementedErrorTb4b4b4(
Ta5d6ff"Ta5d6ffsendfile is broken on SSL transports in Python 3.13Ta5d6ff"Tb4b4b4,
Tb4b4b4)
Tff7b72return Tff7b72await Te6edf3original_sendfileTb4b4b4(
Tff7b72selfTb4b4b4,
Te6edf3transportTb4b4b4,
Te6edf3fileTb4b4b4,
Te6edf3offsetTb4b4b4,
Te6edf3countTb4b4b4,
Te6edf3fallbackTff7b72=Te6edf3fallbackTb4b4b4,
Tb4b4b4)
Te6edf3asyncioTff7b72.Td2a8ffbase_eventsTff7b72.Td2a8ffBaseEventLoopTff7b72.Td2a8ffsendfile Tff7b72= Te6edf3patched_sendfile
Tf0883e@staticmethod
Tff7b72def Td2a8ffset_main_loopTb4b4b4(Te6edf3loopTb4b4b4: Te6edf3asyncioTff7b72.Td2a8ffAbstractEventLoopTb4b4b4)Tb4b4b4:
Te6edf3AsyncUtilsTff7b72.Td2a8ffmain_loop Tff7b72= Te6edf3loop
T8b949e# this method allows running the provided async coroutine from within a sync function
T8b949e# it will run the async function on the main event loop if possible, otherwise it logs a warning
Tf0883e@staticmethod
Tff7b72def Td2a8ffrun_asyncTb4b4b4(Te6edf3coroutineTb4b4b4: Te6edf3CoroutineTb4b4b4)Tb4b4b4:
T8b949e# run provided coroutine on main event loop, ensuring thread safety
Tff7b72if Te6edf3AsyncUtilsTff7b72.Td2a8ffmain_loop Tff7b72and Te6edf3AsyncUtilsTff7b72.Td2a8ffmain_loopTff7b72.Td2a8ffis_runningTb4b4b4(Tb4b4b4)Tb4b4b4:
Te6edf3asyncioTff7b72.Td2a8ffrun_coroutine_threadsafeTb4b4b4(Te6edf3coroutineTb4b4b4, Te6edf3AsyncUtilsTff7b72.Td2a8ffmain_loopTb4b4b4)
Tff7b72return
T8b949e# main event loop not running...
Tffa657printTb4b4b4(Ta5d6ff"Ta5d6ffWARNING: Main event loop not available. Could not schedule task.Ta5d6ff"Tb4b4b4)
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────